package cn.microven.mindmap.controller.menu;

import cn.microven.mindmap.components.node.AbstractMapNode;
import cn.microven.mindmap.oprations.ChangeFontColorOperation;
import cn.microven.mindmap.oprations.ChangeFontSizeOperation;
import cn.microven.mindmap.oprations.ChangeFontTypeOperation;
import cn.microven.mindmap.struture.MindMap;
import javafx.collections.FXCollections;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Slider;
import javafx.scene.control.TextField;

import java.awt.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * 字体样式菜单
 *
 * @author  吴博维
 * @version 1.0
 */
public class FontStyleMenu implements Initializable {

    /**
     * 字体大小文本框
     */
    @FXML
    public TextField fontSizeText;

    /**
     * 字体大小拖动条
     */
    @FXML
    public Slider fontSizeSlider;

    /**
     * 字体颜色选择器
     */
    @FXML
    public ColorPicker fontColor;

    /**
     * 字体选择器
     */
    @FXML
    public ComboBox fontFamily;


    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        fontSizeSlider.setMax(72);
        fontSizeSlider.setMin(8);


        fontSizeSlider.valueProperty().addListener(((observableValue, number, t1) -> {
            fontSizeText.setText(String.valueOf(t1.intValue()));

            if (!MindMap.getInstance().isRedoing() && !MindMap.getInstance().isUndoing()){
                AbstractMapNode selectedNode = MindMap.getInstance().getSelectedNode();
                ChangeFontSizeOperation cfso = new ChangeFontSizeOperation(selectedNode.getFontSize(), t1.intValue(), selectedNode);
                cfso.start();
            }
        }));

        fontSizeText.textProperty().addListener(((observableValue, s, t1) -> {
            if (t1.length() == 0){
                return;
            }
            try {
                fontSizeSlider.setValue(Integer.parseInt(t1));
            }
            catch (NumberFormatException e) {
                fontSizeText.setText(s);
            }
        }));

        fontColor.valueProperty().addListener(((observableValue, color, t1) -> {
            //MindMap.getInstance().getSelectedNode().setFontColor(t1);
            AbstractMapNode selectedNode = MindMap.getInstance().getSelectedNode();
            ChangeFontColorOperation fbco = new ChangeFontColorOperation(selectedNode.getFontColor(),t1,selectedNode);
            fbco.start();
        }));


        /*//获取字体列表 构造Collection
        List<String> fontName= Font.getFontNames();
        ObservableList<String> options =  FXCollections.observableArrayList(fontName);
        //添加字体进入Combo Box
        fontFamily.setItems(options);*/
        //获取字体样式
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        //返回一个数组，其中包含针对指定语言环境本地化的此 GraphicsEnvironment中所有字体系列的名称。
        String[] fontNames = ge.getAvailableFontFamilyNames();
        List<String> textList = new ArrayList(fontNames.length);

        for(int i = 0; i < fontNames.length; ++i) {
            textList.add(new String(fontNames[i]));
        }
        //添加字体进入ComboBox
        this.fontFamily.setItems(FXCollections.observableArrayList(textList));
        fontFamily.valueProperty().addListener(((observableValue, o, t1) -> {
            if (!MindMap.getInstance().isUndoing() && !MindMap.getInstance().isRedoing()) {
                AbstractMapNode selectedNode = MindMap.getInstance().getSelectedNode();
                ChangeFontTypeOperation cfto = new ChangeFontTypeOperation(t1.toString(), selectedNode.getFontFamily(), selectedNode);
                cfto.start();
            }
            //MindMap.getInstance().getSelectedNode().setFontFamily(t1.toString());
        }));
    }

    public void update() {
        fontSizeSlider.setValue(MindMap.getInstance().selectedNodeProperty().getValue().getFontSize());
        fontColor.setValue(MindMap.getInstance().getSelectedNode().getFontColor());
        fontFamily.getSelectionModel().select(MindMap.getInstance().getSelectedNode().getFontFamily());
    }
}
